package com.etc;

import org.apache.hadoop.io.WritableComparable;
import org.apache.log4j.helpers.FormattingInfo;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @Auther: zhezhe
 * @Date: 2018/10/3010:25
 * @Description:
 */
public class ObjectSort implements WritableComparable<ObjectSort> {
    private String orderNum;
    private String OrderName;
    private String goodsName;
    private double goodsPrice;
    private int goodsNum;
    private double totalPrice;

    public ObjectSort(){}

    public ObjectSort(String orderNum,String orderName,String goodsName,double goodsPrice,int goodsNum){
        this.orderNum = orderNum;
        this.OrderName = orderName;
        this.goodsName = goodsName;
        this.goodsPrice = goodsPrice;
        this.goodsNum = goodsNum;
        this.totalPrice = goodsPrice * goodsNum;

    }

    public String getOrderNum() {
        return orderNum;
    }

    public void setOrderNum(String orderNum) {
        this.orderNum = orderNum;
    }

    public String getOrderName() {
        return OrderName;
    }

    public void setOrderName(String orderName) {
        OrderName = orderName;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public double getGoodsPrice() {
        return goodsPrice;
    }

    public void setGoodsPrice(double goodsPrice) {
        this.goodsPrice = goodsPrice;
    }

    public int getGoodsNum() {
        return goodsNum;
    }

    public void setGoodsNum(int goodsNum) {
        this.goodsNum = goodsNum;
    }

    public double getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(double totalPrice) {
        this.totalPrice = totalPrice;
    }


    /**
     * 序列化时调用的方法
     * @param dataOutput
     * @throws IOException
     */

    @Override
    public void write(DataOutput dataOutput) throws IOException {

        dataOutput.writeUTF(this.orderNum);
        dataOutput.writeUTF(this.OrderName);
        dataOutput.writeUTF(this.goodsName);
        dataOutput.writeDouble(this.goodsPrice);
        dataOutput.writeInt(this.goodsNum);
        dataOutput.writeDouble(this.totalPrice);

    }

    /**
     * 反序列化时要调用的方法
     * @param dataInput
     * @throws IOException
     */
    @Override
    public void readFields(DataInput dataInput) throws IOException {

        this.orderNum = dataInput.readUTF();
        this.OrderName = dataInput.readUTF();
        this.goodsName = dataInput.readUTF();
        this.goodsPrice = dataInput.readDouble();
        this.goodsNum = dataInput.readInt();
        this.totalPrice = dataInput.readDouble();
    }

    /**
     * 重写compareto（）方法，该方法用于两个相同数据类型的比较，两个不同类型的数据不能用此方法来比较。
     * 如果指定的数与参数相等返回0。
     * 如果指定的数小于参数返回 -1。
     * 如果指定的数大于参数返回 1。
     * 重写了这个方法后，才能sort排序
     * @param os
     * @return
     */
    @Override
    public int compareTo(ObjectSort os) {
       //返回1说明传进的参数大
        if (this.totalPrice - os.getTotalPrice() < 0){
            return 1;
        }else{
            return -1;
        }
    }

    @Override
    public String toString() {
        return this.orderNum+","+this.OrderName+","+this.goodsName+","+this.goodsPrice+","+this.goodsNum+","+this.totalPrice;
    }
}
